// ==PREPROCESSOR==
// @name "track info + seekbar + buttons"
// @author "marc2003"
// @import "%fb2k_component_path%samples\complete\js\lodash.min.js"
// @import "%fb2k_component_path%samples\complete\js\helpers.js"
// @import "%fb2k_component_path%samples\complete\js\panel.js"
// @import "%fb2k_component_path%samples\complete\js\seekbar.js"
// ==/PREPROCESSOR==

//note the buttons are white and you'd have to edit the images to change them...
var colours = {
	background : _.RGB(30, 30, 30),
	title : _.RGB(255, 255, 255),
	artist : _.RGB(240, 240, 240),
	time : _.RGB(240, 240, 240),
	seekbar_background : _.RGB(160, 160, 160),
	seekbar_progress : _.RGB(255, 255, 255),
	seekbar_knob : _.RGB(196, 30, 35)
};

//////////////////////////////////////////////////////////////

var panel = new _.panel('track info + seekbar + buttons');
var seekbar = new _.seekbar(0, 0, 0, 0);
var buttons = new _.buttons();
var img = null;
var bs = _.scale(24);
on_playback_new_track();

buttons.update = function () {
	var y = Math.round((panel.h - bs) / 2);
	this.buttons.stop = new _.button(panel.w - LM - (bs * 7), y, bs, bs, {normal : 'mono\\appbar.control.stop.png'}, function () { fb.Stop(); }, 'Stop');
	this.buttons.previous = new _.button(panel.w - LM - (bs * 6), y, bs, bs, {normal : 'mono\\appbar.control.rewind.png'}, function () { fb.Prev(); }, 'Previous');
	this.buttons.play = new _.button(panel.w - LM - (bs * 5), y, bs, bs, {normal : !fb.IsPlaying || fb.IsPaused ? 'mono\\appbar.control.play.png' : 'mono\\appbar.control.pause.png'}, function () { fb.PlayOrPause(); }, !fb.IsPlaying || fb.IsPaused ? 'Play' : 'Pause');
	this.buttons.next = new _.button(panel.w - LM - (bs * 4), y, bs, bs, {normal : 'mono\\appbar.control.fastforward.png'}, function () { fb.Next(); }, 'Next');
	this.buttons.console = new _.button(panel.w - LM - (bs * 3), y, bs, bs, {normal : 'mono\\appbar.console.png'}, function () { fb.ShowConsole(); }, 'Console');
	this.buttons.search = new _.button(panel.w - LM - (bs * 2), y, bs, bs, {normal : 'mono\\appbar.magnify.png'}, function () { fb.RunMainMenuCommand('Library/Search'); }, 'Library Search');
	this.buttons.preferences = new _.button(panel.w - LM - bs, y, bs, bs, {normal : 'mono\\appbar.settings.png'}, function () { fb.ShowPreferences(); }, 'Preferences');
}

function on_size() {
	panel.size();
	seekbar.x = Math.round(panel.w * 0.22);
	seekbar.w = panel.w - seekbar.x - _.scale(264);
	seekbar.h = _.scale(12);
	seekbar.y = (panel.h - seekbar.h) / 2;
	buttons.update();
}

function on_paint(gr) {
	gr.FillSolidRect(0, 0, panel.w, panel.h, colours.background);
	buttons.paint(gr);
	gr.FillSolidRect(seekbar.x, seekbar.y, seekbar.w + _.scale(6), seekbar.h, colours.seekbar_background);
	if (fb.IsPlaying) {
		if (img) {
			_.drawImage(gr, img, 0, 0, panel.h, panel.h, image.centre);
		}
		gr.GdiDrawText(_.tfe('%title%'), panel.fonts.title, colours.title, panel.h + 10, 0, seekbar.x - panel.h - _.scale(60), panel.h * 0.6, LEFT);
		gr.GdiDrawText(_.tfe('%artist%'), panel.fonts.normal, colours.artist, panel.h + 10, panel.h * 0.3, seekbar.x - panel.h - _.scale(60), panel.h * 0.7, LEFT);
		gr.SetSmoothingMode(2);
		if (fb.PlaybackLength > 0) {
			var pos = seekbar.pos();
			gr.FillSolidRect(seekbar.x, seekbar.y, pos, seekbar.h, colours.seekbar_progress);
			gr.FillSolidRect(seekbar.x + pos, seekbar.y, _.scale(6), seekbar.h, colours.seekbar_knob);
			gr.GdiDrawText(_.tfe('%playback_time%  '), panel.fonts.normal, colours.time, seekbar.x - _.scale(45), 0, _.scale(45), panel.h, RIGHT);
			gr.GdiDrawText(_.tfe('  %length%'), panel.fonts.normal, colours.time, seekbar.x + seekbar.w + _.scale(6), 0, _.scale(45), panel.h, LEFT);
		}
	}
	gr.DrawRect(seekbar.x, seekbar.y, seekbar.w + _.scale(6), seekbar.h, 1, colours.seekbar_progress);
}

function on_playback_new_track() {
	var metadb = fb.GetNowPlaying();
	if (!metadb) {
		return;
	}
	_.dispose(img);
	img = utils.GetAlbumArtV2(metadb, 0);
	window.Repaint();
}

function on_playback_edited() {
	window.Repaint();
}

function on_playback_seek() {
	seekbar.playback_seek();
}

function on_playback_stop() {
	buttons.update();
	window.Repaint();
}

function on_playback_pause() {
	buttons.update();
	window.Repaint();
}

function on_playback_starting() {
	buttons.update();
	window.Repaint();
}

function on_mouse_wheel(s) {
	if (seekbar.wheel(s)) {
		return;
	}
	if (s == 1) {
		fb.VolumeUp();
	} else {
		fb.VolumeDown();
	}
}

function on_mouse_move(x, y) {
	if (buttons.move(x, y)) {
		return;
	}
	seekbar.move(x, y);
}

function on_mouse_leave() {
	buttons.leave();
}

function on_mouse_lbtn_down(x, y) {
	seekbar.lbtn_down(x, y);
}

function on_mouse_lbtn_up(x, y) {
	if (buttons.lbtn_up(x, y)) {
		return;
	}
	if (seekbar.lbtn_up(x, y)) {
		return;
	}
	fb.RunMainMenuCommand('View/Show now playing in playlist');
}

function on_mouse_rbtn_up(x, y) {
	return panel.rbtn_up(x, y);
}
